/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vehicle.bol;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import vehicle.dal.DAL;
import vehicle.entity.Orders;

/**
 *
 * @author khoangpv_a04581
 */
public class OrdersBol {

    public List<Orders> showOrders(int payment) {

        DAL dal = new DAL();
        List<Orders> listVe = new ArrayList<Orders>();
        String stor = "";
        if (payment == 1) {
            stor = "getAllOrder";
        } else {
            stor = "getAllOrder2";
        }
        try {
            ResultSet rs = dal.executeQuery(stor, new ArrayList());
            while (rs.next()) {
                Orders order = new Orders();
                order.setOrderID(rs.getInt("OrderID"));
                order.setCusFullName(rs.getString("CusFullName"));
                order.setAddress(rs.getString("ADDRESS"));
                order.setEmail(rs.getString("Email"));
                order.setPhoneNumber(rs.getString("PhoneNumber"));
                order.setComments(rs.getString("Comments"));
                order.setPrice(rs.getDouble("Price"));
                order.setQuantity(rs.getInt("Quantity"));
                order.setVehicleID(rs.getInt("VehicleID"));
                order.setDateRegis(rs.getDate("DateRegis"));
                order.setDateOrder(rs.getDate("DateOrder"));
                order.setUserName(rs.getString("UserName"));
                order.setStatus(rs.getInt("STATUS"));
                listVe.add(order);
            }
        } catch (Exception e) {
        } finally {
            dal.closeConnection();
        }
        return listVe;
    }

    public static int insertOrder(Orders ors) {
        DAL dal = new DAL();
        int check = 0;
        try {
            ArrayList paramlist = new ArrayList();
            paramlist.add(ors.getCusFullName());
            paramlist.add(ors.getAddress());
            paramlist.add(ors.getEmail());
            paramlist.add(ors.getPhoneNumber());
            paramlist.add(ors.getComments());
            paramlist.add(ors.getPrice());
            paramlist.add(ors.getQuantity());
            paramlist.add(ors.getVehicleID());
            paramlist.add(ors.getStatus());
            check = dal.executeUpdateQuery("insertOrder", paramlist);
        } catch (Exception ex) {
        } finally {
            dal.closeConnection();
        }

        return check;
    }

    public int paymentOrder(int orderID, String userName) {
        DAL dal = new DAL();
        int check = 0;
        try {
            ArrayList paramlist = new ArrayList();
            paramlist.add(orderID);
            paramlist.add(userName);
            check = dal.executeUpdateQuery("paymentOrder", paramlist);
        } catch (Exception ex) {
        } finally {
            dal.closeConnection();
        }
        return check;
    }

    public int deleteOrder(int orderID) {
        DAL dal = new DAL();
        int check = 0;
        try {
            ArrayList paramlist = new ArrayList();
            paramlist.add(orderID);
            check = dal.executeUpdateQuery("deleteOrder", paramlist);
        } catch (Exception ex) {
        } finally {
            dal.closeConnection();
        }
        return check;
    }

    public List<Orders> searchorderbydateorder(int pail, String form, String to) {

        DAL dal = new DAL();
        List<Orders> listVe = new ArrayList<Orders>();
        ArrayList paramlist = new ArrayList();
        paramlist.add(form);
        paramlist.add(to);
        String stor = "";
        if (pail == 1) {
            stor = "searchorderbydateorder2";
        } else {
            stor = "searchorderbydateorder1";
        }
        try {
            ResultSet rs = dal.executeQuery(stor, paramlist);
            while (rs.next()) {
                Orders order = new Orders();
                order.setOrderID(rs.getInt("OrderID"));
                order.setCusFullName(rs.getString("CusFullName"));
                order.setAddress(rs.getString("ADDRESS"));
                order.setEmail(rs.getString("Email"));
                order.setPhoneNumber(rs.getString("PhoneNumber"));
                order.setComments(rs.getString("Comments"));
                order.setPrice(rs.getDouble("Price"));
                order.setQuantity(rs.getInt("Quantity"));
                order.setVehicleID(rs.getInt("VehicleID"));
                order.setDateRegis(rs.getDate("DateRegis"));
                order.setDateOrder(rs.getDate("DateOrder"));
                order.setUserName(rs.getString("UserName"));
                order.setStatus(rs.getInt("STATUS"));
                listVe.add(order);
            }
        } catch (Exception e) {
        } finally {
            dal.closeConnection();
        }
        return listVe;
    }
}
